$(function() {
    var subData = [];

	var grid = $("#grid").bootgrid(
			{
				post: function () {
			        return {
			        	roleName: $('#nickName').val()
			        } ;
			    },
				ajax: true,
			//	pagination: 3,
				selection: true,
				multiSelect: true,
				sorting: true,
				keepSelection:true,
				rowSelect:true,
				multiSort: true,
				url: ctx + "/admin/admin/roles/page",
				labels: {
					noResults: "对不起，暂无数据！",
					loading: "正在加载数据，请稍后...",
					refresh: "重新加载数据",
					search: "搜索"
				},
			    formatters: {
			        "commands": function(column, row)
			        {
			        	var commands = "<button type=\"button\" class=\"btn btn-xs btn-default glyphicon glyphicon-edit\" data-row-id=\"" + row.roleId + "\"></button> " ;
			        	commands += "<button type=\"button\" class=\"btn btn-xs btn-default glyphicon glyphicon-trash\" data-row-id=\"" + row.roleId + "\"></button>" ;
			        	
			        	return commands ;
			        },
			        "roleName":function(column,row){
			        	var roleName = row.roleName;
			        	return "<a style=\"text-decoration: none; color: black;\" class=\"showDetail\" data-row-id=\"" + row.roleId + "\">"+roleName+"</a>";
			        },
			        "checkDes":function(column,row){
			        	var description = row.description;
			        	if(description.length > 10){
			        		description =  description.substring(0,10)+"...";
			        	}
			        	return description;
			        },
			        "checkPer1":function(column,row){
			        	var permissions = row.permissions;
			        	if(permissions.lastIndexOf(":") > 8){
			        		permissions = permissions.substring(0,10)+"...";
			        	}
			        	return permissions;
			        },
			        "dateFormart" :function(column,row){
			        	var date=null;
			        	if("creatime"==column.id){
				        	date= row.creatime;			        		
			        	}
			        	if("updatetime"==column.id){
				        	date= row.updatetime;			        		
			        	}
			        	if(null == date || "" == date){
			        		return "";
			        	}
			        	var newDate = new Date(date);
			        	return newDate.toLocaleDateString();
			        }
			    }
			}).on("loaded.rs.jquery.bootgrid",function(e) {
				
			    grid.find(".glyphicon-edit").on("click", function(e){
			    	var id = $(this).data("row-id") ;
			    	$('#layoutModal').modal({
						backdrop: 'static' ,
						remote: ctx+'/admin/admin/roles/modify/'+id ,
						show: true
					}) ;
			        
			    }).end().find(".glyphicon-trash").on("click", function(e){
			    	var msg = "<h4><span class='danger'>您确定要删除吗?</span></h4>" ;
					$('#dialogModal .modal-body').html(msg) ;
					$('#dialogModal').modal({
						backdrop: 'static' ,
						show: true
					}) ;
					var id = $(this).data("row-id") ;
					$('#dialogBtn').click(function(){
						XHZ.post(ctx + "/admin/admin/roles/delete",{ids: id}, function(data){
							var msg = "操作成功" ;
							$('#dialogModal').modal('hide') ;
							if(1 == data.code){
								$('.actionBar .glyphicon-refresh').click() ;
							}else{
								msg = data.message ;
							}
							$('#alertModal .modal-body').html(msg) ;
							$('#alertModal').modal({
								backdrop: 'static' ,
								show: true
							}) ;
						}) ;
					}) ;
			    }).end().find(".showDetail").on("click", function(e){
			    	var id = $(this).data("row-id") ;
			    	$('#layoutModal').modal({
						backdrop: 'static' ,
						remote: ctx+'/admin/admin/roles/detail/'+id ,
						show: true
					}) ;
			    });
			}).on("selected.rs.jquery.bootgrid", function(e, selectedRows) {
				var row;
				for ( var i = 0; i < selectedRows.length; i++) {
					row = selectedRows[i];
					subData.push(row.roleId) ;
				}
			}).on("deselected.rs.jquery.bootgrid", function(e, deselectedRows) {
				var row;
				for ( var i = 0; i < deselectedRows.length; i++) {
					var dataIds = subData.join(",") ;
					row = deselectedRows[i];
					dataIds += ',' ;
					dataIds = dataIds.replace(row.roleId+',', '') ;
					subData = dataIds.substring(0,dataIds.length-1).split(',') ;
				}
				
			});
	/**添加新记录**/
	$("button[name=toCreate]").on("click", function() {
		
		$('#layoutModal').modal({
			backdrop: 'static' ,
			remote: ctx+'/admin/admin/roles/create',
			show: true
		}) ;
	});
	/**批量删除**/
	$("button[name=deleteBath]").on("click", function() {
		var msg = "<h4><span class='danger'>请选择要删除的数据.</span></h4>" ;
		if(!subData.join(',')){
			$('#alertModal .modal-body').html(msg) ;
			$('#alertModal').modal({
				backdrop: 'static' ,
				show: true
			}) ;
			
			return false;
		}
		
		msg = "<h4><span class='danger'>您确定要删除吗?</span></h4>" ;
		$('#dialogModal .modal-body').html(msg) ;
		$('#dialogModal').modal({
			backdrop: 'static' ,
			show: true
		}) ;
		$('#dialogBtn').click(function(){
			XHZ.post(ctx + "/admin/admin/roles/delete",{ids: subData.join(',')}, function(data){
				var msg = "操作成功" ;
				$('#dialogModal').modal('hide') ;
				if(1 == data.code){
					subData = [];
					$('.actionBar .glyphicon-refresh').click() ;
				}else{
					msg = data.message ;
				}
				$('#alertModal .modal-body').html(msg) ;
				$('#alertModal').modal({
					backdrop: 'static' ,
					show: true
				}) ;
			}) ;
		}) ;
	});
	/**权限分配**/
	$("button[name=alloc]").on("click", function() {
		var msg = "<h4><span class='danger'>请选择要分配权限的数据.</span></h4>" ;
		if(!subData.join(',')){
			$('#alertModal .modal-body').html(msg) ;
			$('#alertModal').modal({
				backdrop: 'static' ,
				show: true
			}) ;
			return false;
		}
		var ids = [];
		for(var i = 0;i < subData.length;i ++){
			if(null != subData[i]&&''!= subData[i]){
				ids.push(subData[i]);
			}
		}
		$('#layoutModal').modal({
			backdrop: 'static' ,
			remote: ctx+'/admin/admin/roles/toAlloc/'+ids.join(',') ,
			show: true
		}) ;
		
	});
	
});

